// pages/index/index.js
var app = getApp(),
a = app.requirejs("core");
Page({
	data:{
      route: "pintuan",
		    cid:0,
        show:false,
        scrollLeft:0,
        scrollTop:0,
        page:1,
        goodsList:[],
        loading:true,
        suspension:[],
	},
  //底部导航跳转
  menu_url: function (k) {
    a.menu_url(k, 2);
  },
  onLoad: function (e){
    a.setting();
    if (e != null && e != undefined) {
      this.setData({
        tabbar_index: e.tabbar_index ? e.tabbar_index : -1
      });
    }
    this.setData({
      menu: getApp().tabBar,
    })

    if (this.data.tabbar_index >= 0) {
      this.setData({
        showtabbar: true,
      })
    }
    this.systemInfo = wx.getSystemInfoSync()
    this.setIndexData()
    this.setGoodsData()
  },
  onShow:function(){
    
  },
  /**
    * 下拉刷新
    */
  onPullDownRefresh: function () {
    this.setData({
      goodsList: [],
      page: 1,
      loaded: false
    })
    this.setIndexData()
    this.setGoodsData()
    wx.stopPullDownRefresh();
  },
  /**
   *上拉加载
   */
  onReachBottom: function () {
    console.log('加载更多')
    this.data.loaded || this.setGoodsData()
  },
  setIndexData: function () {
    var self = this;
    a.get('Pintuan/ptIndex', {}, function (t) {
      if (t.code == 0) {
        self.setData({
          windowHeight: self.systemInfo.windowHeight,
          advert: t.info.advert,
          category: t.info.cate
        })
      } else {
        a.alert(t.msg);
      }
    })
  },
  setGoodsData:function(){
    if(!this.data.loading){
      return false
    }
    var self = this;
    a.get("Pintuan/ptGoodsList", { page: self.data.page, cate_id: self.data.cid}, function (t) {
      console.log(t)
      if (t.code == 0) {
        var e = {
          loading: false,
        };
          t.info.length > 0 && (e.page = self.data.page + 1, e.goodsList = self.data.goodsList.concat(t.info)),
            t.info.length < 10 && (e.loaded = true);
          self.setData(e)
      } else {
        a.alert(t.msg)
      }
    })
  },
  showList:function(e){

  	var cid = e.currentTarget.dataset.id
  	app.redirect('index/list','cid='+cid)
  },
  showGoodsDetial: function (e) {
    var gid = e.currentTarget.dataset.id
    if (!gid) return;
    app.redirect('goods/detail', 'gid=' + gid)
  },
  switchNav:function(e){
    if(this.data.cid ==e.currentTarget.dataset.cid && e.currentTarget.dataset.cid !=0) return;
    this.data.cid = e.currentTarget.dataset.cid;
    this.data.page = 0
    this.data.loading = true
    this.data.goodsList=[]
    var windowWidth = this.systemInfo.windowWidth
    var offsetLeft = e.currentTarget.offsetLeft
    var scrollLeft = this.data.scrollLeft;
    if(offsetLeft>windowWidth/2){
        scrollLeft = offsetLeft  
    }else{
      scrollLeft = 0
    }
    this.setData({
      goodsList:[],
      childCate:[],
      loading:true,
      scrollLeft:scrollLeft,
      scrollTop:0,
      cid:this.data.cid
    })
    this.setGoodsData()
  },
  scrolltolower:function(e){
    console.log('加载更多')
    this.data.loaded || this.setGoodsData()
  },
      // 拨打电话
    consultation:function(e){
        suspension.call();
    }
})